package com.pp.demo.generator;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * mybatis-plus代码生成器
 */
public class CodeGenerator {

    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
        // 此处输入生成代码对应的表名
        tables.add("user_role");

        String outputBase = System.getProperty("user.dir");
        System.out.println("输出路径：" + outputBase + "/src/main/java");
        FastAutoGenerator.create("jdbc:mysql://124.222.42.79:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&useSSL=false&autoReconnect=true&failOverReadOnly=false&autoReconnectForPools=true&zeroDateTimeBehavior=convertToNull&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai", "root", "80jpnH4.r5g")
                // 全局配置
                .globalConfig(builder -> {
                    builder.author("pzh") //作者
                            .outputDir(outputBase + "/src/main/java") //输出路径(写到java目录)
                            .enableSwagger() //开启swagger
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            .fileOverride() //开启覆盖之前生成的文件
                            .disableOpenDir(); //禁止打开输出目录
                })

                // 包配置
                .packageConfig(builder -> {
                    builder.parent("com.pp.demo.module")
                            .moduleName("user")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, outputBase + "/src/main/resources/mapper/user"));
                })

                // 策略配置
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
//                            .addTablePrefix("")

                            // entity
                            .entityBuilder()
//                            .superClass(BaseEntity.class)
                            .enableLombok()
                            .disableSerialVersionUID()
//                            .enableTableFieldAnnotation()

                            // service
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")

                            // mapper
                            .mapperBuilder()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper")
//                            .enableBaseResultMap()  //生成通用的resultMap

                            // controller
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle();
                })

                // 模板引擎配置
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板

                // 执行
                .execute();
    }

}


